package com.aaa.controller;

import com.aaa.entity.LayData;
import com.aaa.entity.LayTreeData;
import com.aaa.entity.Menu;
import com.aaa.entity.User;
import com.aaa.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有的菜单 并且对应好他们之间的父子关系
     * @param request
     * @return
     */
    @GetMapping("showMenu")
    public String ShowMenu(HttpServletRequest request, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Menu> menus = menuService.showMenu(user);
        request.setAttribute("menu",menus);
        return "main";
    }

    /**
     * 查询除tree所需要的数据   只要ajax LayData
     * @return
     */
    @GetMapping("treeMenuData")
    @ResponseBody
    public LayData getAllMenus(){
        List<LayTreeData> treeDataList =menuService.getAllTreeMenu();
        return new LayData(treeDataList);
    }
    /**
     * 查询该用户的菜单
     */
    @GetMapping("getRoleMenuData/{rid}")
    @ResponseBody
    public LayData getRoleMenuData(@PathVariable Integer rid){
        List<Integer> getRoleMenuDataList = menuService.findRoleMenu(rid);
        LayData layData = new LayData(getRoleMenuDataList);
        return layData;
    }
}
